<?php
namespace app\common\model;

use think\Model;

class Arctype extends Model
{
    static public $allChild = [];   //无限子分类所有ID数组
    
    public function getmodTextAttr($value, $data)
    {
        $arr = config('selectlist.arctype_mod')['data'];
        return $arr[$data['mod']];
    }
    
    public function treeList()
    {
        $list = cache('DB_TREE_ARETYPE');
        if(!$list){
            $list = $this->order('sorts ASC,id ASC')->select();
            foreach ($list as $k => $v){
                $v->mod_text = $v->mod_text;
                $v->typelink = type_link($v);
            }
            $treeClass = new \expand\Tree();
            $list = $treeClass->create($list);
            cache('DB_TREE_ARETYPE', $list);
        }
        return $list;
    }
    
    /**
     * @Title: channelData
     * @Description: todo(当前pid的平级分类)
     * @param int $pid
     * @return array
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function channeldata($pid)
    {
        $result = cache('DB_ARCTYPE_PID_'.$pid);
        if(!$result){
            $where = [
                ['pid', '=', $pid],
                ['status', '=', 1],
            ];
            $result = $this->where($where)->order('sorts ASC,id ASC')->select();
            foreach ($result as $k =>$v){
                $result[$k]['typelink'] = type_link($v);
            }
            cache('DB_ARCTYPE_PID_'.$pid, $result);
        }
        return $result;
    }
    
    /**
     * @Title: channel
     * @Description: todo(分类html)
     * @param int $pid
     * @param int $nowid
     * @param string $ishome
     * @param string $leftlabel
     * @param string $rightlabel
     * @param string $class
     * @return string
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月29日
     * @throws
     */
    public function channel($pid, $nowid, $ishome, $leftlabel, $rightlabel, $class)
    {
        $data = $this->channeldata($pid);
        $result = '';
        if($ishome != ''){
            if ($nowid == '' || empty($nowid)){
                $classs = "class=\"active ".$class."\"";
            }else{
                $classs = "class=\"".$class."\"";
            }
            $result .= $leftlabel;
            $result .= "<a href=\"".url('/')."\" ".$classs." >".$ishome."</a>";
            $result .= $rightlabel;
        }
        foreach ($data as $k => $v){
            if ($v['id'] == $nowid){
                $classs = "class=\"active ".$class."\"";
            }else{
                $classs = "class=\"".$class."\"";
            }
            $result .= $leftlabel;
            $result .= "<a href=\"".$v['typelink']."\" ".$classs." target=".$v['target']." >".$v['typename']."</a>";
            $result .= $rightlabel;
        }
        return $result;
    }
    
    /**
     * @Title: arctypeField
     * @Description: todo(分类字段)
     * @param int $id
     * @param string $key
     * @return string
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function arctypeField($id, $key)
    {
        $result = $this->where('id', $id)->value($key);
        return $result;
    }
    
    /**
     * @Title: typename
     * @Description: todo(分类名称)
     * @param int $id
     * @return string
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function typename($id)
    {
        return $this->arctypeField($id, 'typename');
    }
    
    /**
     * @Title: typelink
     * @Description: todo(分类链接)
     * @param int $id
     * @return string
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function typelink($id)
    {
        $data = $this->field('mod,jumplink,dirs')->where('id', $id)->find();
        $result = type_link($data);
        return $result;
    }
    
    /**
     * @Title: position
     * @Description: todo(当前位置)
     * @param int $id
     * @param string $home
     * @return string
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function position($id, $home)
    {
        $positionArr = [];
        $positionArr = $this->positionArctype($id);
        $positionArr = array_reverse($positionArr);
        $result = "<a href=\"".url("/")."\">".$home."</a>";
        $num = count($positionArr) - 1;
        foreach($positionArr as $k=>$val){
            $result .= '<a href="'.type_link($val).'">'.$val['typename'].'</a>';
        }
        return $result;
    }
    public function positionArctype($id, $result=[])
    {
        $data = $this->where('id', $id)->find();
        if($data['pid'] == '0'){
            $result[] = $data;
        }else{
            $result[] = $data;
            return $this->positionArctype($data['pid'], $result);
        }
        return $result;
    }
    /********************************************-系统方法[尽量勿修改]-********************************************/
    /**
     * @Title: topArctypeData
     * @Description: todo(查询顶级分类)
     * @param int $pid
     * @return array
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function topArctypeData($pid)
    {
        $data = $this->where([['id', '=', $pid]])->find();
        if($data['pid'] == '0'){
            $result = $data;
        }else{
            return $this->topArctypeData($data['pid']);
        }
        return $result;
    }
    
    /**
     * @Title: allChildArctype
     * @Description: todo(查询当前分类下无限级分级的所有ID)
     * @param int $id
     * @return array
     * @author 刀客源码 <654108442@qq.com>
     * @date 2018年11月9日
     * @throws
     */
    public function allChildArctype($id)
    {
        self::$allChild[] = $id;
        $where = [
            ['pid', '=', $id],
            ['status', '=', '1'],
        ];
        $data = $this->field('id')->where($where)->select();
        if(!empty($data)){
            foreach($data as $k=>$v){
                $this->allChildArctype($v['id']);
            }
        }
        return self::$allChild;
    }
    /********************************************-系统方法[尽量勿修改]-********************************************/
}